package com.cxg.fairy.ec.detail;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;

import butterknife.BindView;

/**
 * Created by gabrielsung on 2018/4/6 0006.
 */

public class PicStandardTabDelegate extends LatteDelegate{

    private static final String ARG_GOODS_ID = "ARG_GOODS_ID";

    @BindView(R2.id.tab_layout_ps)
    TabLayout mTabLayout = null;

    @BindView(R2.id.view_pager_ps)
    ViewPager mViewPager = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_goods_detail_pic_standard;
    }

    public static PicStandardTabDelegate create(int goodsId) {
        final Bundle args = new Bundle();
        args.putInt(ARG_GOODS_ID, goodsId);
        final PicStandardTabDelegate delegate = new PicStandardTabDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        initData();
        initTabLayout();
    }

    private void initTabLayout() {
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setBackgroundColor(Color.WHITE);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    private void initPager(JSONObject data) {
        final PicStandardTabPagerAdapter adapter = new PicStandardTabPagerAdapter(getFragmentManager(), data);
        mViewPager.setAdapter(adapter);
    }

    private void initData() {
        RestClient.builder()
                .url("getGoodsDetail")
                .params("skuId",getArguments().getInt(ARG_GOODS_ID))
                .params("uuid", AccountManager.getSignUUID())
                .loader(getContext())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        final JSONObject data = JSON.parseObject(response).getJSONObject("data");
                        initPager(data);
                    }
                })
                .build()
                .post();
    }

}
